#include "PulseAudioUtils.h"


void createPulseSampleSpecification(pa_sample_spec *sample_spec, 
                                    pa_sample_format_t format, 
                                    uint32_t rate, uint8_t channels) {
    sample_spec->format = format;
    sample_spec->rate = rate;
    sample_spec->channels = channels;
}

bool createPulseSimpleRecord(pa_simple **sample, pa_sample_spec *sample_spec) {
    int error;
    *sample = pa_simple_new(NULL, "RecordAPP", PA_STREAM_RECORD, NULL, "Record", sample_spec, NULL, NULL, &error);
    if (*sample == NULL) {
        INFO_LOG("%s Fail: %s", __func__, pa_strerror(error));
    }
    return *sample != NULL;
}


bool createPulseSimplePlay(pa_simple **sample, pa_sample_spec *sample_spec) {
    int error;
    *sample = pa_simple_new(NULL, "PlayAPP", PA_STREAM_PLAYBACK, NULL, "PlayBack", sample_spec, NULL, NULL, &error);
    if (*sample == NULL) {
        INFO_LOG("%s Fail: %s", __func__, pa_strerror(error));
    }
    return *sample != NULL;
}


void destroyPulseSimple(pa_simple **simple) {
    if (*simple != NULL) {
        pa_simple_free(*simple);
    }
}

bool readPulseSimple(pa_simple *simple, char *buffer, size_t buffer_size) {
    return pa_simple_read(simple, buffer, buffer_size, NULL) == 0;
}

bool writePulseSimple(pa_simple *simple, char *buffer, size_t buffer_size) {
    return pa_simple_write(simple, buffer, buffer_size, NULL) == 0;
}

bool waitPulseSimplePlayed(pa_simple **simple) {
    int error;
    return pa_simple_drain(*simple, &error) == 0;
}
